# -*- coding:utf-8 -*-
import sys
import os
from pykafka import KafkaClient
from pykafka.common import OffsetType
import sqlconnect
import bluemessage
import blemessage
import wifimessage
import bb
import pymysql
import json,threading
import time
#import urllib2
#from bs4 import BeautifulSoup
#import time



client = KafkaClient(hosts="localhost:9092")
topic =client.topics['logstashmain']
# 从最新的偏移量拉取数据
consumer = topic.get_simple_consumer(consumer_group="logstashmain",auto_offset_reset=OffsetType.LATEST,reset_offset_on_start=True)
partitions = topic.partitions
#consumer.reset_offsets([(partitions[0], 150)])
last_offset = topic.latest_available_offsets()
# 消费Kafaka 中 Topic:logstashmain 的数据



def kafaka():
    print("begin......kafkaserver")
    wifimessage.changewifiDeviceStatus()
    bluemessage.updatestatus()

    for message in consumer:

        if message is not None:
            #扫描出来蓝牙数据，将str转换成dic进行处理
            #print message.value
            #s=eval(message.value)
            s = json.loads(message.value)
            #print(s)
            if s['interface']=='Ble':
                bluemessage.updatestatus()
                interface = s['interface']
                aa = eval(str(s['bleinfo']))
               # print type(aa),aa
                rssi = aa['rssi']
                hop = aa['hops']
                asset_mac = aa['asset_mac']
                bluemessage.deletebymac(aa['asset_mac'])
                #blemessage.deletebymac(asset_mac)
                #ip = aa['evtsrc_ip']
                flags = aa['Flags']
                services = str(aa['services']).replace("'",'"')
               # print services
                create_time = s['evt_dev_time']
                asset_type = aa['asset_type']
                detail = aa['detail']
                port = '无'
                protocols = '无'
                status = 1
                db = sqlconnect.connect()
                blecorsor = db.cursor()
                sql = "INSERT INTO blue_info(interface,status,rssi,hop,asset_mac,flags,services,evt_dev_time,asset_type,detail)" \
                      " VALUES('%s','%d','%s','%s','%s','%s','%s','%s','%s','%s') " %(interface,status, rssi, hop, asset_mac, flags, services, create_time, asset_type, detail)
                # blecorsor.execute(sql)
                try:
                    # Execute the SQL command
                    blecorsor.execute(sql)
                    # Commit your changes in the database
                    db.commit()
                    print "insert Ble success"
                except:
                    print "insert error!!!!"
                    db.rollback()
                # disconnect from server
                db.close()
            elif s['interface'] == 'Classic Blue':
                try:

                    bluemessage.deletebymac(s['asset_mac'])
                    interface = s['interface']
                    hop = 2
                    asset_mac = s['asset_mac'].encode("utf-8")

                    ip = s['evtsrc_ip'].encode("utf-8")
                    create_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
                    asset_type = s['asset_type'].encode("utf-8")
                    detail = s['detail'].decode('unicode_escape')
                    print detail
                    status = s['status'].encode("utf-8")
                   # print s['status'],type(s['status'].encode("utf-8"))
                    if status=='on':
                        online = 1
                    else:
                        online = 0
                    brand = 'unknown'
                    if s['pro'].encode("utf-8") == 'unknown':
                        pro = []
                    else:
                        pro = s['pro'].encode("utf-8")
                    #pro = ",".join(str(i) for i in s['pro'])
                    #print s['pro'],type(s['pro'].encode("utf-8"))
                    if s['ports'].encode("utf-8") == '"unknown"':
                        ports = 0
                    else:
                        ports = s['ports'].encode("utf-8")

                    asset_ip = s['evtsrc_ip'].encode("utf-8")
                    #model = s['model']
                    #protocols = s['protocols'].encode("utf-8")
                    if s['protocols'].encode("utf-8") == 'unknown':
                        protocols = 'unknown'
                    else:
                        protocols = s['protocols'].encode("utf-8")
                    #print asset_mac,asset_type,detail,status,pro,ports,type(protocols),protocols
                    db = sqlconnect.connect()
                    bluecorsor = db.cursor()
                    sql = "INSERT INTO blue_info(interface,asset_mac,protocols,hops,detail,pro,ports,asset_type,evt_dev_time,status)" \
                          "VALUES ('%s','%s','%s','%d','%s','%s','%s','%s','%s','%d')" %(interface,asset_mac,protocols,2,detail,
                                                                                                  pro,ports,asset_type,create_time,online)
                    #print sql
                    bluecorsor.execute(sql)
                    db.commit()
                    print "insert Blue success", asset_mac
                    # disconnect from server
                    db.close()
                except:
                    pass
            elif s['interface'] == 'WifiDevice':

                interface = s['interface']

                apList = eval(str(s['apList']).replace('null','None'))
                create_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
                #print apList,interface
                for k,v in apList.items():
                    #print k,v
                    #create_time = s['evt_dev_time']
                    asset_mac = k
                    wifimessage.deletewifidevice(asset_mac)
                    name = v[0]
                    channel = v[1]
                    encrpy = str(v[2]).replace("'", '"')
                    avg_signal = v[3]
                    min_signal = v[4]
                    max_signal = v[5]
                    db = sqlconnect.connect()
                    wificorsor = db.cursor()
                    sql = "INSERT INTO wifiDevice(interface, asset_mac, name, channel, encrpy, avg_signal, min_signal, max_signal,create_time,status,tag)" \
                          "VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')" %('WifiDevice',asset_mac,name,channel,encrpy,avg_signal,min_signal,max_signal,create_time,1,1)
                    wificorsor.execute(sql)
                    try:
                        # Execute the SQL command

                        # Commit your changes in the database
                        db.commit()
                        print "insert-------------wifi------------ success",name
                    except:
                        print "insert error!!!!"
                        db.rollback()
                    # disconnect from server
                    db.close()
            elif s['interface'] == 'notAssociationStation':
                wifimessage.deletefromNotAStation()
                interface = s['interface']
                create_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
                # print type(s['notAList'])
                wifimac= str(s['notAList']).split(',')
                if eval(s['notAList']):
                    for i in eval(s['notAList']):
                        #print mac
                       # wifimessage.deletenotAStation(i)
                        db = sqlconnect.connect()
                        notAcorsor = db.cursor()
                        sql = "INSERT INTO not_A_Station(interface,mac,create_time) VALUES ('%s','%s','%s')" %(interface, i, create_time)
                        #sql = notAcorsor.execute(sql)
                        try:
                            # Execute the SQL command
                            notAcorsor.execute(sql)
                            # Commit your changes in the database
                            db.commit()
                            print "insert not_A_Station success"
                        except:
                            print "insert error!!!!"
                            db.rollback()
                        # disconnect from server
                    db.close()
            elif s['interface'] == 'AssociationStation':
                wifimessage.deletefromAStation()
                interface = s['interface']
                create_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
                aa=eval(s['AList'])
                #print aa
                for k,v in aa.items():
                    Ap = k.split(';')[1]
                    Ap_name = k.split(';')[0]
                    for i in v:
                        AClient = i
                        wifimessage.deleteAassication(AClient)
                        db = sqlconnect.connect()
                        Acorsor = db.cursor()
                        sql = "INSERT INTO wifi_A_Station(interface,AClient,Ap,Ap_name,create_time) VALUES ('%s','%s','%s','%s','%s')" % (
                        interface, AClient,Ap,Ap_name, create_time)
                        # sql = notAcorsor.execute(sql)
                        try:
                            # Execute the SQL command
                            Acorsor.execute(sql)
                            # Commit your changes in the database
                            db.commit()
                            print "insert A_Station success",AClient
                        except:
                            print "insert error!!!!"
                            db.rollback()
                        # disconnect from server
                        db.close()
            elif s['interface'] == 'probssidd':
                interface = s['interface']
                create_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
                aa = eval(s['probSsid'])
                print aa
                for i in aa:
                    wifimessage.deletewifiprobSsid(i['SSID'])
                    db = sqlconnect.connect()
                    probcorsor = db.cursor()
                    print i['CLIENT'],i['SSID']
                    sql = "INSERT INTO wifi_probSsid(interface,wifi_name,client,create_time) VALUES ('%s','%s','%s','%s')" % (
                    interface, i['SSID'],i['CLIENT'], create_time)
                    # sql = notAcorsor.execute(sql)
                    try:
                        # Execute the SQL command
                        probcorsor.execute(sql)
                        # Commit your changes in the database
                        db.commit()
                        print "insert prob success"
                    except:
                        print "insert error!!!!"
                        db.rollback()
                    # disconnect from server
                    db.close()


            elif s['interface'] == 'BleFuzzScan':
                interface = s['interface']
                bleattack = eval(s['bleFuzz'])
                attack_type = bleattack["attack_type"]
                master = bleattack["master"]
                slave = bleattack["slave"]
                scan_time = bleattack["time"]
                print attack_type,master,slave,scan_time
                db = sqlconnect.connect()
                bleattackcorsor = db.cursor()
                sql = "INSERT INTO bleAttackScan(master, slave, scan_time, interface,attack_type)" \
                      " VALUES('%s','%s','%s','%s','%s')" % (master, slave, scan_time,interface,attack_type)
                #bleattackcorsor.execute(sql)
                try:
                    # Execute the SQL command
                    bleattackcorsor.execute(sql)
                    # Commit your changes in the database
                    db.commit()
                    print "insert bleAttackScan success"
                except:
                    print "insert error!!!!"
                    db.rollback()
                # disconnect from server
                db.close()
            elif s['interface'] == 'blueAttackScan':
                interface = s['interface']
                Blueattack = s["Blueattack"]
                aa=eval(str(Blueattack))
                attack_type = aa['attack_type']
                print aa
                scan_time = aa['time']
                slave = aa['slave'].upper()
                master = aa['master'].upper()
                #print len(attack_type),type(str(attack_type))
                #bluemessage.deleteblueAttack(slave, attack_type)
                db = sqlconnect.connect()
                blecorsor = db.cursor()
                sql = "INSERT INTO blueAttackScan(interface, attack_type, scan_time, slave,master)" \
                      " VALUES('%s','%s','%s','%s','%s')" %(interface, attack_type, scan_time, slave,master)
                #blecorsor.execute(sql)
                try:
                    # Execute the SQL command
                    blecorsor.execute(sql)
                    # Commit your changes in the database
                    db.commit()
                    print "insert blueAttack success -----",attack_type,'----'
                except:
                    print "insert error!!!!"
                    db.rollback()
                # disconnect from server
                db.close()
            elif s['interface']=='wifiDoss':
                wifimessage.deleteBeaconAttackfromwifiDoss()
                interface = s['interface']
                wifiDoss = s['wifiDoss']
                create_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
                aa = eval(wifiDoss)
               # print aa

                Dos_type = aa['type']
                victim = aa['victim']

                db = sqlconnect.connect()
                wifidosscorsor = db.cursor()
                sql = "INSERT INTO wifi_Doss(interface, Dos_type, create_time, victim)" \
                      " VALUES('%s','%s','%s','%s')" % (interface, Dos_type, create_time, victim)
               # wifidosscorsor.execute(sql)
                try:
                    # Execute the SQL command
                    wifidosscorsor.execute(sql)
                    # Commit your changes in the database
                    db.commit()
                    print "insert success----wifi Doss----",Dos_type,'---------'
                except:
                    print "insert error!!!!"
                    db.rollback()
                # disconnect from server
                db.close()
            elif s['interface'] == 'fakeAp':
                interface = s['interface']
                fakeap =  eval(s['fakeapm'])
                create_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
                fakemessage = fakeap[1]
                fakename = fakeap[0]
                db = sqlconnect.connect()
                fake = db.cursor()
                sql = "INSERT INTO wifi_fakeap(interface, fake_M, create_time, fakename)" \
                      " values ('%s', '%s', '%s', '%s')" % (interface, fakemessage, create_time,fakename)
                try:
                    fake.execute(sql)
                    db.commit()
                    print "insert fakeAP success", fakename
                except:
                    print "insert fakeAp error!!!!!!!!!!"
                    db.rollback()
                db.close()
            elif s['interface'] == 'router':
                #print s
                wifimessage.deleterouterleakByasset_mac(s['routermac'])
                interface = s['interface']
                asset_name = s['routermac']
                leak_message = s['routerleakMessage']
               # print leak_message
                create_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
                db = sqlconnect.connect()
                leak = db.cursor()
                sql = "INSERT INTO router_leak(interface,asset_mac,leak_message,create_time) values " \
                      "('%s', '%s', '%s', '%s')" %(interface,asset_name,leak_message,create_time)
                leak.execute(sql)
                try:
                    #
                    db.commit()
                    print "insert routerLeak success",asset_name
                except:
                    print "insert routerleak error!!!!!!!!"
                    db.rollback()
                db.close()
            elif s['interface'] == 'blueleak':
                interface = s['interface']
                blueleakmessage = eval(s['blueleakmessage'])
                print blueleakmessage
                for i in blueleakmessage:
                    bluemessage.deleteblueleak(i[0].upper(),i[1])
                for i in blueleakmessage:
                    create_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
                    b_message = i[1]
                    asset_mac = i[0].upper()
                    #bluemessage.deleteblueleak(asset_mac)
                    db = sqlconnect.connect()
                    leak = db.cursor()
                    sql = "INSERT INTO b_leak_m(b_message,asset_mac,create_time) values ('%s', '%s', '%s')" %(b_message,asset_mac,create_time)
                    try:
                        leak.execute(sql)
                        db.commit()
                        print "\ninsert Blue--Leak success", asset_mac
                    except:
                        print "\ninsert routerleak error!!!!!!!!"
                        db.rollback()
                    db.close()

                print blueleakmessage[0], type(blueleakmessage),type(eval(s['blueleakmessage']))
            elif s['interface'] == 'wifileak':
                interface = s['interface']
                wifileakmessage = eval(s['wifileakmessage'])
                print wifileakmessage

                wifimessage.deleterouterleakByasset_mac(wifileakmessage[0].upper())

                create_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
                leak_message = wifileakmessage[1]
                interface = 'wifileak'
                asset_mac = wifileakmessage[0].upper()
                db = sqlconnect.connect()
                leak = db.cursor()
                sql = "INSERT INTO router_leak(interface,asset_mac,leak_message,create_time) values " \
                      "('%s', '%s', '%s', '%s')" % (interface, asset_mac, leak_message, create_time)
                try:
                    leak.execute(sql)
                    db.commit()
                    print "insert wifiLeak success", asset_mac
                except:
                    print "insert wifileak error!!!!!!!!"
                    db.rollback()
                db.close()

threads = []
threads.append(threading.Thread(target=kafaka))
#threads.append(threading.Thread(target=bb.accesszigbee))
if __name__ == '__main__':
    for t in threads:
        t.start()